/**
 * 
 */
package com.lanswon.qzsmk.controller.cardManageController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.SystemException;

/**
 * @author sun
 *
 */
@ControllerAdvice
public class BaseController {

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Map<String, Object> handlerException(HttpServletRequest req,
			HttpServletResponse res, Throwable e) {
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", 300);
		e.printStackTrace();
		boolean isAjax = "XMLHttpRequest" == req.getHeader("X-Requested-With");
		if (isAjax) {
		      res.setContentType("application/json");
		    }
		if (e instanceof ApplicationException) {
			//res.setStatus(500);
			int errorCode = ((ApplicationException) e).getErrorCode();
			switch (errorCode) {
			case 1: map.put("message", "数据格式错误"); break;
			case 2: map.put("message", "无异常数据"); break;
			case 3: map.put("message", "普通异常"); break;
			case 4: map.put("message", "主要参数为空"); break;
			case 5: map.put("message", e.getMessage()); break;
			case 6: map.put("message", "个人已经拥有此卡产品"); break;
			case 7: map.put("message", "重复数据"); break;
			case 8: map.put("message", "数据不合法"); break;
			case 9: map.put("message", "数据不匹配"); break;
			case 10: map.put("message", "用户不存在"); break;
			case 11: map.put("message", "密码错误"); break;
			case 12: map.put("message", "角色已绑定用户"); break;
			case 13: map.put("message", "角色已绑定权限"); break;
			case 14: map.put("message", "网点已经存在"); break;
			case 15: map.put("message", "角色已经存在"); break;
			case 16: map.put("message", "登陆名已经存在"); break;
			case 17: map.put("message", "用户未绑定柜员"); break;
			case 18: map.put("message", "收费定义已经过期或未定义"); break;
			case 19: map.put("message", "此卡产品已在使用"); break;
			case 20: map.put("message", e.getMessage()); break;
			case 21: map.put("message", "此卡号段已有卡号在使用"); break;
			case 22: map.put("message", "btc文件和sq文件记录数不符合"); break;
			case 23: map.put("message", "文件卡号有重复"); break;
			case 24: map.put("message", e.getMessage()); break;
			case 25: map.put("message", e.getMessage()); break;
			case 26: map.put("message", e.getMessage()); break;
			case 27: map.put("message", e.getMessage()); break;
			case 28: map.put("message", e.getMessage()); break;
			case 29: map.put("message", "证件号码或者手机号码不能为空"); break;
			default:
				break;
			}
		}else if(e instanceof SystemException) {
			map.put("message", e.getMessage());
		}else if(e instanceof Exception){
			map.put("message", "系统操作异常，请联系维护人员");
		}
		return map;
	}

}
